﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.IO;
using System.Diagnostics;
using Microsoft;


public partial class Admin_Mail_Details : System.Web.UI.Page
{
    SqlConnection con;
    string MailID;
    float totalfee;
    float chargeWeight;
    float chargeDeliver;
    float distance;
    //Connect to database   
    void connect()
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        con.Open();
        //if (con != null)
        //{
        //    Response.Write("Connect Successful.");
        //}
       
    }
    //Load  from database to form
    void loadData() 
    {
        try
        {
            MailID = Convert.ToString(Request.QueryString["MailID"]);
            string sqlMail = "SELECT [MailID] ,[BranchID] ,[DistanceID] ,[MTypeID],[DTypeID],[WeightID],[ZipCode],[DateReceipt],[DateDelivery],[Status],[isSendEmail],[SenderName],[SenderAddress],[SenderEmail],[SenderPhone],[ReceiverName],[ReceiverAddess],[ReceiverPhone],[TotalFee] FROM [Mail] WHERE ([MailID] = @MailID)";
            SqlCommand cmdMail = new SqlCommand(sqlMail, con);
            cmdMail.Parameters.AddWithValue("@MailID", MailID);
            SqlDataAdapter da = new SqlDataAdapter(cmdMail);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count != 0)
            {
                lbMailID.Text = dt.Rows[0][0].ToString();
                ddlBranch.Text = dt.Rows[0][1].ToString();
                ddlDOMail.Text = dt.Rows[0][2].ToString();
                ddlTOMail.Text = dt.Rows[0][3].ToString();
                ddlTODeliver.Text = dt.Rows[0][4].ToString();
                ddlWOMail.Text = dt.Rows[0][5].ToString();
                txtZipCode.Text = dt.Rows[0][6].ToString();
                bdpDateReceipt.SelectedDate = DateTime.Parse(dt.Rows[0][7].ToString());
                if (dt.Rows[0][8].ToString() != "Pending")
                {
                    bdpDateDelivery.SelectedDate = DateTime.Parse(dt.Rows[0][8].ToString());
                }       
                ddlStatus.Text = dt.Rows[0][9].ToString();
                ddlSendEmail.Text = dt.Rows[0][10].ToString();
                txtSName.Text = dt.Rows[0][11].ToString();
                txtSAddress.Text = dt.Rows[0][12].ToString();
                txtSEmail.Text = dt.Rows[0][13].ToString();
                txtSPhone.Text = dt.Rows[0][14].ToString();
                txtRName.Text = dt.Rows[0][15].ToString();
                txtRAddress.Text = dt.Rows[0][16].ToString();
                txtRPhone.Text = dt.Rows[0][17].ToString();
                lbTotalFee.Text = dt.Rows[0][18].ToString();
            }
        }catch(Exception e)
        {
            MessageDialog.MessDialog(e.Message, this);
        }
        
    }
    // Update from form to database
    void update() 
    {
        try
        {
            MailID = Convert.ToString(Request.QueryString["MailID"]);
            string sqlUpdateMail = "UPDATE [Mail] SET [BranchID] = @BranchID, [DistanceID] = @DistanceID, [MTypeID] = @MTypeID, [DTypeID] = @DTypeID, [WeightID] = @WeightID,     [ZipCode] = @ZipCode, [DateDelivery] = @DateDelivery, [Status] = @Status, [isSendEmail] = @isSendEmail, [SenderName] = @SenderName, [SenderAddress] = @SenderAddress, [SenderEmail] = @SenderEmail, [SenderPhone] = @SenderPhone, [ReceiverName] = @ReceiverName, [ReceiverAddess] = @ReceiverAddess, [ReceiverPhone] = @ReceiverPhone, [TotalFee] = @TotalFee WHERE [MailID] = @MailID";
            SqlCommand cmdUpdate = new SqlCommand(sqlUpdateMail, con);
            cmdUpdate.Parameters.AddWithValue("MailID", MailID);
            cmdUpdate.Parameters.AddWithValue("@BranchID", ddlBranch.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@DistanceID", ddlDOMail.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@MTypeID", ddlTOMail.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@DTypeID", ddlTODeliver.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@WeightID", ddlWOMail.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@ZipCode", txtZipCode.Text);
            cmdUpdate.Parameters.AddWithValue("@DateDelivery", bdpDateDelivery.SelectedDate.ToString("MM/dd/yyyy"));
            cmdUpdate.Parameters.AddWithValue("@Status", ddlStatus.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@isSendEmail", ddlSendEmail.SelectedValue.ToString());
            cmdUpdate.Parameters.AddWithValue("@SenderName", txtSName.Text);
            cmdUpdate.Parameters.AddWithValue("@SenderAddress", txtSAddress.Text);
            cmdUpdate.Parameters.AddWithValue("@SenderEmail", txtSEmail.Text);
            cmdUpdate.Parameters.AddWithValue("@SenderPhone", txtSPhone.Text);
            cmdUpdate.Parameters.AddWithValue("@ReceiverName", txtRName.Text);
            cmdUpdate.Parameters.AddWithValue("@ReceiverAddess", txtRAddress.Text);
            cmdUpdate.Parameters.AddWithValue("@ReceiverPhone", txtRPhone.Text);
            cmdUpdate.Parameters.AddWithValue("@TotalFee", lbTotalFee.Text);
            int Count = cmdUpdate.ExecuteNonQuery();
            if (Count > 0)
            {
                MessageDialog.MessDialog("Update Mail Completed", this);
            }
            else
            {
                MessageDialog.MessDialog("Update Mail Failed", this);
            }
        }catch(Exception e)
        {
            MessageDialog.MessDialog(e.Message, this);
        }
    
    }
    //Total Fee
    void total()
    {
        int WID;//ID Weight of Mail
        int TID;// ID Type of Deliver
        int DID;//ID Distanceof mail 

        WID = Convert.ToInt16(ddlWOMail.SelectedValue);
        TID = Convert.ToInt16(ddlTODeliver.SelectedValue);
        DID = Convert.ToInt16(ddlDOMail.SelectedValue);
        SqlCommand cmd = new SqlCommand("Select WeightCharge from WeightOfMail where WeightID= '" + WID + "'", con);
        SqlDataReader dread = cmd.ExecuteReader();
        if (dread.Read())
        {
            chargeWeight = float.Parse(dread.GetValue(0).ToString());
          
        }
        dread.Close();
        SqlCommand cmd1 = new SqlCommand("Select DTypeCharge from TypeofDeliver where DTypeID= '" + TID + "'", con);
        SqlDataReader dread1 = cmd1.ExecuteReader();
        if (dread1.Read())
        {
            chargeDeliver = float.Parse(dread1.GetValue(0).ToString());
          
        }
        dread1.Close();
        SqlCommand cmd2 = new SqlCommand("Select DistanceCharge from DistanceofMail where DistanceID= '" + DID + "'", con);
        SqlDataReader dread2 = cmd2.ExecuteReader();
        if (dread2.Read())
        {
            distance = float.Parse(dread2.GetValue(0).ToString());
          
        }
        dread2.Close();
        lbTotalFee.Text = Convert.ToString((chargeWeight + chargeDeliver) * distance);

        // string total==Convert.ToString((float.Parse(ddlTODeliver.SelectedValue) + float.Parse(charge)) * float.Parse(ddlDOMail.SelectedValue));
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        connect();
     
        if (this.IsPostBack == false)
        {
            loadData();
            Session["IDEmail"] = lbMailID.Text;
            Session["Email"] = txtSEmail.Text;
            Session["SenderName"] = txtSName.Text;
        }
      
      
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        update();
    }
   
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
       
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        total();
    }
}